//
//  XYSearchView.m
//  OC_weibo
//
//  Created by 李旭阳 on 16/1/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "XYSearchView.h"

@interface XYSearchView()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textFieldRigthCon;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@end


@implementation XYSearchView

+ (instancetype)searchView{
    return [[[NSBundle mainBundle] loadNibNamed:@"SearchView" owner:nil options:nil] firstObject];
}

-(void)awakeFromNib{
    
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    //让图片按原比例显示
    leftView.contentMode = UIViewContentModeCenter;
    
    leftView.width = self.textField.height;
    leftView.height = self.textField.height;
    self.textField.leftView = leftView;
    //默认left 不显示 设置始终显示
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.delegate = self;
}

- (IBAction)cancelButtonClick:(id)sender {
    
    //辞去第一响应者
    [self endEditing:YES];
    self.textFieldRigthCon.constant = 0;
    [UIView animateWithDuration:0.25 animations:^{
        //刷新视图 更新约束 布局
        [self layoutIfNeeded];
    }];
    
}


#pragma mark textField 代理方法
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    self.textFieldRigthCon.constant = self.cancelButton.width;
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

@end
